package com.app.micaihu.view.shop.adapter;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.viewpager.widget.PagerAdapter;

import com.app.micaihu.R;
import com.app.micaihu.bean.news.GalleryEntity;
import com.app.micaihu.bean.news.ImageDetailBean;
import com.app.micaihu.bean.news.RecommendGalleryEntity;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.custom.view.PhotoImageView;
import com.app.micaihu.view.newsdetail.view.ImageAboutView;
import com.app.utils.util.ViewSizeUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;

import java.util.List;

/**
 * Created by Guo on 2017/4/24.
 *
 * 商品图文的 adapter(viewpager
 */

public class CommoditPicAdapter extends PagerAdapter implements View.OnClickListener, View.OnLongClickListener {

    private List<GalleryEntity> galleryList;
    private List<RecommendGalleryEntity> recommendGalleryList;

    private String nextArticleId;

    //    private OnPhotoViewClickListener mOnPhotoViewClickListener;
    private ImageAboutView view;
    private ImageDetailBean imageDetailBean;
    private PhotoImageView.ImagePhotoViewListener imagePhotoViewListener;



    public CommoditPicAdapter(ImageDetailBean imageDetailBean){

        this.imageDetailBean = imageDetailBean;
        if(imageDetailBean != null){
            this.galleryList = imageDetailBean.getGalleryList();
            this.recommendGalleryList = imageDetailBean.getRecommendGallery();
            this.nextArticleId = imageDetailBean.getNextArticleId();
        }
    }



    @Override
    public int getCount() {
        if(galleryList == null || galleryList.size()==0){//图片集合为空,返回0
            return 0;
        }else if(recommendGalleryList==null || recommendGalleryList.size()==0 ){//推荐集合为空,返回图片集合的size
            return galleryList.size();
        }else{//都不为空,返回图片+1的sizie
//            int i = galleryList.size() + 1;
            //LogUtils.e("gzq","viewpager的页码"+i+"");
            return galleryList.size() + 1;
        }

    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        //首先,推荐的list不能为空,并且已经划到最后一页,否则,就不创建gridview的视图
        if(recommendGalleryList != null && position == galleryList.size()){
            view = new ImageAboutView(container.getContext(),recommendGalleryList,nextArticleId);
            container.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return view;
        }else{
            PhotoView photoView = new PhotoView(container.getContext(), galleryList.get(position).getGalleryUrl());
            photoView.setOnClickListener(this);
            photoView.setOnLongClickListener(this);
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return photoView;



        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //super.destroyItem(container, position, object);
        container.removeView((View) object);
    }

    public  void setImageClickListener(PhotoImageView.ImagePhotoViewListener imagePhotoViewListener){

        this.imagePhotoViewListener = imagePhotoViewListener;
    }


    @Override
    public void onClick(View v) {
        if(imagePhotoViewListener != null){
            imagePhotoViewListener.onOneStepClick();
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if(imagePhotoViewListener != null){
            imagePhotoViewListener.onLongStepClick();
        }
        return false;
    }

    /**
     * 返回ImageAboutView的方法
     * @return
     */
    public  ImageAboutView getImageAboutView(){
        return view;
    }





    public  class PhotoView extends FrameLayout{

        private String url;

        public PhotoView(Context context) {
            super(context);
        }

        public PhotoView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public PhotoView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        public PhotoView(Context context,String url){
            super(context);
            this.url = url;
            init(context);
        }


        private void init(Context context){
            inflate(context, R.layout.view_photo_shop,this);
            final CustomImageView pic = (CustomImageView) findViewById(R.id.civ);

            LoadImageUtils.getInstance().loadImage(pic, url, new ControllerListener<ImageInfo>() {
                @Override
                public void onSubmit(String id, Object callerContext) {

                }

                @Override
                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                    if (imageInfo != null) {
                        int height = imageInfo.getHeight();
                        int width = imageInfo.getWidth();

                        if(imageInfo.getWidth() > ViewSizeUtils.getWidthPixels()){

                            float rate = (float) width/(float)height;
                            pic.setMaxWidth(ViewSizeUtils.getWidthPixels());
                            float aspectRatio = pic.getAspectRatio();
                            if(aspectRatio != rate){
                                pic.setAspectRatio(rate);
                            }
//                            LogUtils.e("gzq", "宽更宽"+"   asp = "+rate + "  width = "+width+"   height = "+height);

                        }else if(imageInfo.getHeight() > ViewSizeUtils.getHeightPixels()){
                            float rate = (float) width/(float)height;
                            pic.setMaxHeight(ViewSizeUtils.getHeightPixels());
                            float aspectRatio = pic.getAspectRatio();
                            if(aspectRatio != rate){
                                pic.setAspectRatio(rate);
                            }
//                            LogUtils.e("gzq", "高更高"+"   asp = "+rate + "  width = "+width+"   height = "+height);
                        } else{
                            ViewGroup.LayoutParams layoutParams = pic.getLayoutParams();
                            layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                            layoutParams.width = ViewSizeUtils.getWidthPixels();
                            pic.setLayoutParams(layoutParams);
                            float rate = (float) width/(float)height;
                            pic.setAspectRatio(rate);
//                            LogUtils.e("gzq", "正常"+"   asp = "+rate + "  width = "+width+"   height = "+height);
                        }
                    }
                }

                @Override
                public void onIntermediateImageSet(String id, ImageInfo imageInfo) {

                }

                @Override
                public void onIntermediateImageFailed(String id, Throwable throwable) {

                }

                @Override
                public void onFailure(String id, Throwable throwable) {

                }

                @Override
                public void onRelease(String id) {

                }
            });

        }



    }

}
